package ru.isokolov.lib;

public class FioDeclination {
    private final String[] ends = new String[7];

    private void setEndings(String c1, String c2, String c3, String c4, String c5, String c6) {
        this.ends[1] = c1;
        this.ends[2] = c2;
        this.ends[3] = c3;
        this.ends[4] = c4;
        this.ends[5] = c5;
        this.ends[6] = c6;
    }

    private void clearEndings() {
        this.setEndings("", "", "", "", "", "");
    }

    private String leaveWithoutDeclension(String lastName, String firstName, String middleName) {
        StringBuilder result = new StringBuilder();
        if (firstName.isEmpty()) {
            result.append(lastName).append(' ').append(middleName);
        } else if (firstName.charAt(firstName.length() - 1) == '.') {
            result.append(lastName).append(' ').append(firstName).append(middleName);
        } else {
            result.append(lastName).append(' ').append(firstName).append(' ').append(middleName);
        }

        return FioComm.proper(result.toString()).trim();
    }

    public boolean nonDeclension(String anyWord, boolean male, boolean isLastName, boolean multiple) {
        if (anyWord.isEmpty()) {
            return true;
        } else {
            int len = anyWord.length();
            char last = anyWord.charAt(len - 1);
            if ("аоуыэяеёюи".indexOf(last) >= 0 && "аяиы".indexOf(last) < 0) {
                return true;
            } else {
                if (isLastName) {
                    if (len <= 2) {
                        return true;
                    }

                    if (multiple) {
                        if (anyWord.equalsIgnoreCase("Бут") || anyWord.equalsIgnoreCase("Тер") || anyWord.equalsIgnoreCase("Аскер") || anyWord.equalsIgnoreCase("Кара") || anyWord.equalsIgnoreCase("Бонч") || anyWord.equalsIgnoreCase("Куй")) {
                            return true;
                        }

                        if (ExceptionDict.exceptionDic.present(anyWord, "FirstPartLastName")) {
                            return true;
                        }
                    }

                    String cEnd = anyWord.substring(len - 2);
                    if (cEnd.equals("иа") || cEnd.equals("ия")) {
                        return true;
                    }

                    if (male) {
                        if (last == 1080 && !anyWord.startsWith("ск", len - 3) || last == 1099 && anyWord.charAt(len - 2) != 1085) {
                            return true;
                        }

                        if ((cEnd.equals("их") || cEnd.equals("ых")) && !ExceptionDict.exceptionDic.present(anyWord, "BaseNonRussian")) {
                            return true;
                        }
                    } else {
                        if ("бвгджзйклмнпрстфхцчшщ".indexOf(last) >= 0 || "ьиы".indexOf(last) >= 0) {
                            return true;
                        }

                        if (ExceptionDict.exceptionDic.present(anyWord, "LastNameW")) {
                            return true;
                        }
                    }

                    return ExceptionDict.exceptionDic.present(anyWord, "LastName");
                } else {
                    if ("иы".indexOf(last) >= 0) {
                        return true;
                    }

                    if (male) {
                        return ExceptionDict.exceptionDic.present(anyWord, "FirstNameM");
                    } else {
                        if ("бвгджзйклмнпрстфхцчшщ".indexOf(last) >= 0) {
                            return true;
                        }

                        return ExceptionDict.exceptionDic.present(anyWord, "FirstNameW");
                    }
                }
            }
        }
    }

    public String getFirstNameM(String firstName, int declination) {
        if (firstName.isEmpty()) {
            return "";
        } else {
            firstName = FioComm.proper(firstName);
            String result = firstName;
            if (firstName.equalsIgnoreCase("Пётр") && declination >= 2) {
                firstName = firstName.replace('ё', 'е');
            }

            int len = firstName.length();
            if (declination > 1 && firstName.charAt(len - 1) != '.') {
                if (firstName.charAt(len - 1) == 1086) {
                    firstName = FioComm.proper(ExceptionDict.exceptionDic.getRightPart(firstName, "FirstNameParallelForms"));
                    len = firstName.length();
                }

                if (this.nonDeclension(firstName, true, false, false)) {
                    return result;
                }

                char last = firstName.charAt(len - 1);
                this.clearEndings();
                if (firstName.equalsIgnoreCase("Лев")) {
                    firstName = "Льв";
                    this.setEndings("", "а", "у", "а", "ом", "е");
                } else if (firstName.equalsIgnoreCase("Павел")) {
                    firstName = "Павл";
                    this.setEndings("", "а", "у", "а", "ом", "е");
                } else {
                    char prev = firstName.length() > 1 ? firstName.charAt(len - 2) : 32;
                    if (last != 1081 && last != 1100) {
                        if (last == 1072 && "бвгджзйклмнпрстфхцчшщ".indexOf(prev) >= 0) {
                            this.setEndings("а", "ы", "е", "у", "ой", "е");
                            if ("гкх".indexOf(prev) >= 0 || "чщ".indexOf(prev) >= 0 || "жш".indexOf(prev) >= 0) {
                                this.ends[2] = "и";
                            }

                            if ("цш".indexOf(prev) >= 0 || "чщ".indexOf(prev) >= 0 || 1078 == prev) {
                                this.ends[5] = "ей";
                            }

                            if (len > 2) {
                                firstName = firstName.substring(0, len - 1);
                            }
                        } else if (last == 1103 && ("бвгджзйклмнпрстфхцчшщ".indexOf(prev) >= 0 || 1100 == prev)) {
                            this.setEndings("я", "и", "е", "ю", "ей", "е");
                            if (len > 2) {
                                firstName = firstName.substring(0, len - 1);
                            }
                        } else if ("бвгджзйклмнпрстфхцчшщ".indexOf(last) >= 0) {
                            this.setEndings("", "а", "у", "а", "ом", "е");
                        }
                    } else {
                        this.setEndings(String.valueOf(last), "я", "ю", "я", "ем", "е");
                        if (prev == 1080) {
                            this.ends[6] = "и";
                        }

                        firstName = firstName.substring(0, len - 1);
                    }

                    if ("цш".indexOf(last) >= 0 || "чщ".indexOf(last) >= 0 || 1078 == last) {
                        this.ends[5] = "ем";
                    }
                }

                result = firstName + this.ends[declination];
            }

            return result;
        }
    }

    private String getFirstNameW(String firstName, int declination) {
        if (firstName.isEmpty()) {
            return "";
        } else {
            firstName = FioComm.proper(firstName);
            String result = firstName;
            int len = firstName.length();
            if (declination > 1 && firstName.charAt(len - 1) != '.') {
                if (this.nonDeclension(firstName, false, false, false)) {
                    return firstName;
                }

                this.clearEndings();
                char last = firstName.charAt(len - 1);
                char prev = firstName.length() > 1 ? firstName.charAt(len - 2) : 32;
                if (last != 1072) {
                    if (last == 1103) {
                        this.setEndings("я", "и", "е", "ю", "ей", "е");
                        if (prev == 1080 && ExceptionDict.exceptionDic.accentInfo(firstName) != 1) {
                            this.ends[3] = "и";
                            this.ends[6] = "и";
                        }

                        firstName = firstName.substring(0, len - 1);
                    } else if (last == 1100) {
                        this.setEndings("ь", "и", "и", "ь", "ью", "и");
                        firstName = firstName.substring(0, len - 1);
                    }
                } else {
                    this.setEndings("а", "ы", "е", "у", "ой", "е");
                    if ("гкх".indexOf(prev) >= 0 || "чщ".indexOf(prev) >= 0 || "жш".indexOf(prev) >= 0) {
                        this.ends[2] = "и";
                    }

                    if ("цш".indexOf(prev) >= 0 || "чщ".indexOf(prev) >= 0 || 1078 == prev) {
                        this.ends[5] = "ей";
                    }

                    firstName = firstName.substring(0, len - 1);
                }

                result = firstName + this.ends[declination];
            }

            return result;
        }
    }

    private String getMiddleNameM(String middleName, int declination) {
        if (middleName.isEmpty()) {
            return "";
        } else {
            middleName = FioComm.proper(middleName);
            String result = middleName;
            int len = middleName.length();
            char last = middleName.charAt(len - 1);
            if (declination > 1 && last != '.') {
                if (FioComm.getSex(middleName) == 1084) {
                    this.setEndings("", "а", "у", "а", "ем", "е");
                    if (last != 1095) {
                        this.clearEndings();
                    }

                    result = middleName + this.ends[declination];
                } else {
                    result = this.getFirstNameM(middleName, declination);
                }
            }

            return result;
        }
    }

    private String getMiddleNameW(String middleName, int declination) {
        if (middleName.isEmpty()) {
            return "";
        } else {
            middleName = FioComm.proper(middleName);
            String result = middleName;
            int len = middleName.length();
            char last = middleName.charAt(len - 1);
            if (declination > 1 && last != '.') {
                if (FioComm.getSex(middleName) == 1078) {
                    this.setEndings("а", "ы", "е", "у", "ой", "е");
                    if (last == 1072) {
                        middleName = middleName.substring(0, len - 1);
                    } else {
                        this.clearEndings();
                    }

                    result = middleName + this.ends[declination];
                } else {
                    result = this.getFirstNameW(middleName, declination);
                }
            }

            return result;
        }
    }

    private String getLastNameM(String lastName, int declination, boolean multiple) {
        if (lastName.isEmpty()) {
            return "";
        } else {
            String result;
            int len = lastName.length();
            int delimitPos = lastName.indexOf(45);
            if (delimitPos >= 0) {
                int count = StrUtils.wordCount(lastName, "-");
                String fstPart = lastName.substring(0, delimitPos).trim();
                String endPart = lastName.substring(delimitPos + 1).trim();
                if (count == 2 && endPart.toLowerCase().indexOf("оглы") >= 0) {
                    result = StrUtils.properCase(fstPart, " ") + '-' + endPart;
                } else {
                    result = this.getLastNameM(fstPart, declination, true) + '-' + this.getLastNameM(endPart, declination, false);
                }
            } else {
                lastName = FioComm.proper(lastName);
                this.clearEndings();
                result = lastName;
                if (declination > 1) {
                    if (this.nonDeclension(lastName, true, true, multiple)) {
                        return lastName;
                    }

                    String cEnd = lastName.length() < 3 ? lastName : lastName.substring(len - 3);
                    if (cEnd.equals("ски")) {
                        lastName = lastName + 'й';
                        cEnd = "ий";
                        ++len;
                    }

                    cEnd = lastName.length() < 2 ? lastName : lastName.substring(len - 2);
                    if (cEnd.equals("ны")) {
                        lastName = lastName + 'й';
                        cEnd = "ый";
                        ++len;
                    }

                    if (cEnd.equals("ый")) {
                        this.setEndings("", "ого", "ому", "ого", "ым", "ом");
                        lastName = lastName.substring(0, len - 2);
                    } else {
                        char last;
                        if (cEnd.equals("ой")) {
                            this.setEndings("", "ого", "ому", "ого", "ым", "ом");
                            last = lastName.charAt(len - 3);
                            if (Syllable.countSyllable(lastName) != 1 && last != 1088) {
                                if ("гкх".indexOf(last) >= 0 || "чщ".indexOf(last) >= 0) {
                                    this.ends[5] = "им";
                                }

                                lastName = lastName.substring(0, len - 2);
                            } else {
                                this.setEndings("й", "я", "ю", "я", "ем", "е");
                                lastName = lastName.substring(0, len - 1);
                            }
                        } else if (!cEnd.equals("ий") && !cEnd.equals("ей") && !cEnd.equals("ай") && !cEnd.equals("яй") && !cEnd.equals("уй")) {
                            if (!cEnd.equals("ын") && !cEnd.equals("ин") && !cEnd.equals("ев") && !cEnd.equals("ёв") && !cEnd.equals("ов")) {
                                if (!cEnd.equals("ич") && !cEnd.equals("ач")) {
                                    if (cEnd.equals("ок")) {
                                        this.setEndings("", "а", "у", "а", "ом", "е");
                                        if (Syllable.countSyllable(lastName) > 1 && !ExceptionDict.exceptionDic.present(lastName, "NonLeaveVocalic") && lastName.charAt(len - 3) != 1082 && "бвгджзйклмнпрстфхцчшщ".indexOf(lastName.charAt(len - 4)) < 0) {
                                            lastName = lastName.substring(0, len - 2) + 'к';
                                        }
                                    } else if (cEnd.equals("ец")) {
                                        this.setEndings("", "а", "у", "а", "ем", "е");
                                        if (!ExceptionDict.exceptionDic.present(lastName, "NonLeaveVocalic")) {
                                            this.ends[5] = "ом";
                                            switch (Character.toLowerCase(lastName.charAt(len - 3))) {
                                                case 'а':
                                                case 'е':
                                                case 'и':
                                                case 'о':
                                                case 'у':
                                                    this.ends[5] = "ем";
                                                    lastName = lastName.substring(0, len - 2) + "йц";
                                                    break;
                                                case 'б':
                                                case 'в':
                                                case 'г':
                                                case 'ж':
                                                case 'з':
                                                case 'й':
                                                case 'к':
                                                case 'м':
                                                case 'р':
                                                case 'с':
                                                case 'т':
                                                default:
                                                    if (Syllable.countSyllable(lastName) > 1) {
                                                        lastName = lastName.substring(0, len - 2) + 'ц';
                                                    }
                                                    break;
                                                case 'д':
                                                case 'н':
                                                    if (len > 3 && "аоуыэяеёюи".indexOf(lastName.charAt(len - 4)) >= 0 && Syllable.countSyllable(lastName) > 2) {
                                                        lastName = lastName.substring(0, len - 2) + 'ц';
                                                    }
                                                    break;
                                                case 'л':
                                                    if (len > 3) {
                                                        if ("аоуыэяеёюи".indexOf(lastName.charAt(len - 4)) >= 0) {
                                                            lastName = lastName.substring(0, len - 2) + "ьц";
                                                        } else {
                                                            this.ends[5] = "ем";
                                                            if (lastName.charAt(len - 4) == 1081) {
                                                                this.ends[5] = "ом";
                                                            }
                                                        }
                                                    }
                                                    break;
                                                case 'п':
                                                    if (len > 3) {
                                                        if ("аоуыэяеёюи".indexOf(lastName.charAt(len - 4)) >= 0) {
                                                            this.ends[5] = "ем";
                                                            lastName = lastName.substring(0, len - 2) + 'ц';
                                                        }
                                                    } else {
                                                        this.ends[5] = "ем";
                                                    }
                                            }

                                            if (ExceptionDict.exceptionDic.accentInfo(result) == 2) {
                                                this.ends[5] = "ем";
                                            }
                                        }
                                    } else {
                                        last = lastName.charAt(len - 1);
                                        char prev = lastName.length() > 1 ? lastName.charAt(len - 2) : 32;
                                        if (last == 1072 && "бвгджзйклмнпрстфхцчшщ".indexOf(prev) >= 0) {
                                            this.setEndings("а", "ы", "е", "у", "ой", "е");
                                            if ("гкх".indexOf(prev) >= 0 || "чщ".indexOf(prev) >= 0 || "жш".indexOf(prev) >= 0) {
                                                this.ends[2] = "и";
                                            }

                                            if (("чщ".indexOf(prev) >= 0 || "цш".indexOf(prev) >= 0 || 1078 == prev) && ExceptionDict.exceptionDic.accentInfo(lastName) != 1) {
                                                this.ends[5] = "ей";
                                            }

                                            lastName = lastName.substring(0, len - 1);
                                        } else if (last == 1103 && "бвгджзйклмнпрстфхцчшщь".indexOf(prev) >= 0) {
                                            this.setEndings("я", "и", "е", "ю", "ей", "е");
                                            lastName = lastName.substring(0, len - 1);
                                        } else if (last == 1100) {
                                            this.setEndings("", "я", "ю", "я", "ем", "е");
                                            lastName = lastName.substring(0, len - 1);
                                        } else if ("бвгджзйклмнпрстфхцчшщ".indexOf(last) >= 0) {
                                            this.setEndings("", "а", "у", "а", "ом", "е");
                                            if ("цш".indexOf(last) >= 0) {
                                                this.ends[5] = "ем";
                                            }
                                        }
                                    }
                                } else {
                                    this.setEndings("", "а", "у", "а", "ем", "е");
                                }
                            } else {
                                this.setEndings("", "а", "у", "а", "ым", "е");
                                if (lastName.equalsIgnoreCase("Лев")) {
                                    lastName = "Льв";
                                }

                                if (Syllable.countSyllable(lastName) == 1) {
                                    this.ends[5] = "ом";
                                }

                                if (lastName.equalsIgnoreCase("Львов")) {
                                    this.ends[5] = "ым";
                                }

                                if (ExceptionDict.exceptionDic.present(lastName, "BaseNonRussian")) {
                                    this.ends[5] = "ом";
                                }
                            }
                        } else {
                            last = lastName.charAt(len - 3);
                            if (cEnd.equals("ий") && "гкх".indexOf(last) >= 0) {
                                this.setEndings(cEnd, "ого", "ому", "ого", "им", "ом");
                                lastName = lastName.substring(0, len - 2);
                            } else if (!cEnd.equals("ий") || "цш".indexOf(last) < 0 && "чщ".indexOf(last) < 0 && "нж".indexOf(last) < 0) {
                                this.setEndings("й", "я", "ю", "я", "ем", "е");
                                lastName = lastName.substring(0, len - 1);
                            } else {
                                this.setEndings(cEnd, "его", "ему", "его", "им", "ем");
                                lastName = lastName.substring(0, len - 2);
                            }
                        }
                    }

                    result = lastName + this.ends[declination];
                }

            }
            return result;
        }
    }

    private String getLastNameW(String lastName, int declination, boolean multiple) {
        if (lastName.isEmpty()) {
            return "";
        } else {
            String result = "";
            int len = lastName.length();
            int delimitPos = lastName.indexOf(45);
            if (delimitPos >= 0) {
                int count = StrUtils.wordCount(lastName, "-");
                String fstPart = lastName.substring(0, delimitPos).trim();
                String endPart = lastName.substring(delimitPos + 1).trim();
                if (count == 2 && endPart.toLowerCase().contains("кызы")) {
                    result = StrUtils.properCase(fstPart, " ") + '-' + endPart;
                } else {
                    result = this.getLastNameW(fstPart, declination, true) + '-' + this.getLastNameW(endPart, declination, false);
                }
            } else {
                lastName = FioComm.proper(lastName);
                this.clearEndings();
                result = lastName;
                if (declination > 1) {
                    if (this.nonDeclension(lastName, false, true, multiple)) {
                        return lastName;
                    }

                    String cEnd = lastName.length() > 3 ? lastName.substring(len - 4) : lastName;
                    if (cEnd.equals("цына")) {
                        this.setEndings("а", "ой", "ой", "у", "ой", "ой");
                        lastName = lastName.substring(0, len - 1);
                    } else {
                        cEnd = lastName.length() > 2 ? lastName.substring(len - 3) : lastName;
                        if (cEnd.equals("ска") && "ьв".indexOf(lastName.charAt(len - 4)) >= 0 || cEnd.equals("цка")) {
                            lastName = lastName + 'я';
                            ++len;
                        }

                        if (!cEnd.equals("ова") && !cEnd.equals("ева") && !cEnd.equals("ёва") && !cEnd.equals("ина")) {
                            cEnd = lastName.length() > 1 ? lastName.substring(len - 2) : lastName;
                            char prev;
                            if (!cEnd.equals("ая") && !cEnd.equals("яя")) {
                                cEnd = lastName.substring(len - 1);
                                if (cEnd.equals("а") && "бвгджзйклмнпрстфхцчшщ".indexOf(lastName.charAt(len - 2)) >= 0) {
                                    this.setEndings("а", "ы", "е", "у", "ой", "е");
                                    prev = lastName.charAt(len - 2);
                                    if ("гкх".indexOf(prev) >= 0 || "чщ".indexOf(prev) >= 0 || "жш".indexOf(prev) >= 0) {
                                        this.ends[2] = "и";
                                    }

                                    if (("цш".indexOf(prev) >= 0 || "чщ".indexOf(prev) >= 0 || 1078 == prev) && ExceptionDict.exceptionDic.accentInfo(lastName) != 1) {
                                        this.ends[5] = "ей";
                                    }

                                    lastName = lastName.substring(0, len - 1);
                                } else if (cEnd.equals("я") && "бвгджзйклмнпрстфхцчшщь".indexOf(lastName.charAt(len - 2)) >= 0) {
                                    this.setEndings("я", "и", "е", "ю", "ей", "е");
                                    lastName = lastName.substring(0, len - 1);
                                }
                            } else {
                                if (cEnd.equals("ая")) {
                                    this.setEndings("ая", "ой", "ой", "ую", "ой", "ой");
                                    prev = lastName.charAt(len - 3);
                                    if ("цш".indexOf(prev) >= 0 || "чщ".indexOf(prev) >= 0 || 1078 == prev) {
                                        this.setEndings("ая", "ей", "ей", "ую", "ей", "ей");
                                    }
                                } else {
                                    this.setEndings("яя", "ей", "ей", "юю", "ей", "ей");
                                }

                                lastName = lastName.substring(0, len - 2);
                            }
                        } else {
                            if (ExceptionDict.exceptionDic.present(lastName, "DependedLastNameW")) {
                                this.setEndings("а", "ы", "е", "у", "ой", "е");
                            } else {
                                this.setEndings("а", "ой", "ой", "у", "ой", "ой");
                            }

                            lastName = lastName.substring(0, len - 1);
                        }
                    }

                    result = lastName + this.ends[declination];
                }

            }
            return result;
        }
    }

    public String getFIO(String lastName, String firstName, String middleName, char sex, int declination) {
        if (lastName == null) {
            lastName = "";
        } else {
            lastName = lastName.trim();
        }

        if (firstName == null) {
            firstName = "";
        } else {
            firstName = firstName.trim();
        }

        if (middleName == null) {
            middleName = "";
        } else {
            middleName = middleName.trim();
        }

        boolean flagPoint = false;
        StringBuilder result = new StringBuilder();
        if (!FioComm.isRangeInt(1, 6, declination)) {
            throw FioComm.createError(-1, String.valueOf(declination));
        } else {
            int midLen = middleName.length();
            if (midLen > 0 && middleName.charAt(midLen - 1) == '.') {
                String tmpS = middleName.substring(0, midLen - 1);
                if (FioComm.getSex(tmpS) != 0 || FioComm.chinaName(lastName, firstName, tmpS)) {
                    middleName = tmpS;
                    --midLen;
                    flagPoint = true;
                }
            }

            char declinationType;
            if (sex == 0) {
                if (midLen <= 0) {
                    return this.leaveWithoutDeclension(lastName, firstName, middleName);
                }

                if (middleName.charAt(midLen - 1) == '.') {
                    return this.leaveWithoutDeclension(lastName, firstName, middleName);
                }

                declinationType = FioComm.getSex(middleName);
                if (declinationType != 0) {
                    sex = declinationType;
                } else {
                    sex = 1;
                }
            }

            sex = Character.toLowerCase(sex);
            if (midLen > 0) {
                if (middleName.charAt(midLen - 1) != '.') {
                    declinationType = FioComm.getSex(middleName);
                } else {
                    declinationType = sex;
                }

                if (declinationType == 0) {
                    if (FioComm.chinaName(lastName, firstName, middleName)) {
                        declinationType = 'c';
                    } else {
                        declinationType = sex;
                    }
                }
            } else {
                declinationType = sex;
            }

            switch (sex) {
                case 'ж':
                    if (declinationType == 1078) {
                        result.append(this.getLastNameW(lastName, declination, false)).append(' ').append(this.getFirstNameW(firstName, declination));
                        if (midLen > 0) {
                            if (result.charAt(result.length() - 1) != '.' || middleName.charAt(midLen - 1) != '.') {
                                result.append(' ');
                            }

                            result.append(this.getMiddleNameW(middleName, declination));
                        }
                    } else {
                        result.append(this.leaveWithoutDeclension(lastName, firstName, middleName));
                    }
                    break;
                case 'м':
                    switch (declinationType) {
                        case 'c':
                            middleName = middleName.toLowerCase();
                            if ("бвгджзйклмнпрстфхцчшщь".indexOf(middleName.charAt(midLen - 1)) >= 0) {
                                FioComm.proper(result.append(lastName).append(' ').append(firstName)).append(' ').append(this.getFirstNameM(middleName, declination));
                            } else {
                                result.append(this.leaveWithoutDeclension(lastName, firstName, middleName));
                            }

                            return flagPoint ? result.toString().trim() + '.' : result.toString().trim();
                        case 'ж':
                            result.append(this.leaveWithoutDeclension(lastName, firstName, middleName));
                            return flagPoint ? result.toString().trim() + '.' : result.toString().trim();
                        case 'м':
                            result.append(this.getLastNameM(lastName, declination, false)).append(' ').append(this.getFirstNameM(firstName, declination));
                            if (midLen > 0) {
                                if (result.charAt(result.length() - 1) != '.' || middleName.charAt(midLen - 1) != '.') {
                                    result.append(' ');
                                }

                                result.append(this.getMiddleNameM(middleName, declination));
                            }

                            return flagPoint ? result.toString().trim() + '.' : result.toString().trim();
                        default:
                            result.append(this.leaveWithoutDeclension(lastName, firstName, middleName));
                            return flagPoint ? result.toString().trim() + '.' : result.toString().trim();
                    }
                default:
                    if (sex != 1) {
                        throw FioComm.createError(-2, "'" + sex + "'");
                    }

                    result.append(this.leaveWithoutDeclension(lastName, firstName, middleName));
            }

            return flagPoint ? result.toString().trim() + '.' : result.toString().trim();
        }
    }
}
